package com.sch.example.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sch.calendar.CalendarView;
import com.sch.calendar.adapter.VagueAdapter;
import com.sch.calendar.entity.Date;
import com.sch.calendar.listener.OnMonthChangedListener;
import com.sch.calendar.util.DateUtil;
import com.sch.example.R;
import com.sch.example.adapter.MyVagueAdapter;
import com.sch.example.checkin.Checkin;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;


/**
 * 日历Window
 */
public class CalendarWindow extends PopupWindow {

    // 通用
    private Context mContext;

    // 日历适配器相关
    private VagueAdapter<Map<String, Map<String, Checkin>>> mVagueAdapter;
    private CalendarView mCalendarView;
    private View mCurrView;
    private int mLastDay = 0;
    private boolean b = true;

    // 点击布局
    private RelativeLayout mRlStartTime, mRlRepeat, mRlSubmit;
    private TextView mTvRepeat, mTvStartTime;
    private ImageView mImgClose;

    /**
     * 确定键监听器
     */
    public void setOnPositiveListener(View.OnClickListener listener) {
        mRlSubmit.setOnClickListener(listener);
        mRlStartTime.setOnClickListener(listener);
        mRlRepeat.setOnClickListener(listener);
    }


    /**
     * 构造方法
     */
    public CalendarWindow(Activity mContext) {
        super(mContext);
        View view = LayoutInflater.from(mContext).inflate(R.layout.window_calendar, null);

        this.mContext = mContext;
        this.setContentView(view);                                 // 设置SelectPicPopupWindow的View
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);       // 设置SelectPicPopupWindow弹出窗体的高
        this.setAnimationStyle(R.style.AnimBottom);                // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setTouchable(true);                                   // 这个控制PopupWindow内部控件的点击事件
        this.setFocusable(true);                                   // 设置SelectPicPopupWindow弹出窗体可点击
        this.setBackgroundDrawable(new ColorDrawable(0xb0000000)); // 设置SelectPicPopupWindow弹出窗体的背景

//        this.setOutsideTouchable(true);
//        this.update();

        initCalendarView(view);
    }




    /**
     * 获取年
     */
    public int getYear() {
        return mSelectYear;
    }

    /**
     * 获取月
     */
    public int getMonth() {
        return mSelectMonth;
    }

    /**
     * 获取日
     */
    public int getDay() {
        return mSelectDay;
    }


    /**
     * 获取日期
     */
    public String getDate() {
        return mSelectYear + "-" +
                mSelectMonth + "-" +
                mSelectDay;
    }


    // 今年/今月/今日
    private int mThisYear = DateUtil.today().getYear();
    private int mThisMonth = DateUtil.today().getMonth() + 1;
    private int mThisDay = DateUtil.today().getDayOfMonth();

    // 选中年月日
    private int mSelectYear;
    private int mSelectMonth;
    private int mSelectDay;


    public void setRepeat(String tv_repeat) {
        mTvRepeat.setText(tv_repeat);
    }

    public void setStartTime(String tv_start_time) {
        mTvStartTime.setText(tv_start_time);
    }

    private void initCalendarView(View view) {

        mImgClose = view.findViewById(R.id.img_close);
        mImgClose.setOnClickListener(view1 -> dismiss());
        mRlStartTime = view.findViewById(R.id.rl_start_time);
        mRlRepeat = view.findViewById(R.id.rl_repeat);
        mRlSubmit = view.findViewById(R.id.rl_submit);

        mTvRepeat = view.findViewById(R.id.tv_repeat);
        mTvStartTime = view.findViewById(R.id.tv_start_time);


        mCalendarView = view.findViewById(R.id.calendar_view);
        mCalendarView.setCanDrag(true);           // 不能通过幻灯片更改月份
        mCalendarView.setScaleEnable(false);      // 月份更改后无法自动缩放日历
        mCalendarView.setShowOverflowDate(false); // 隐藏显示蒙特的溢出日期
        mCalendarView.setCanFling(true);
        mCalendarView.setTitleFormat("yyyy年MM月", Locale.CHINA);
        mCalendarView.setOnMonthChangedListener(new OnMonthChangedListener() {
            @Override
            public void onMonthChanged(Date date) {
//                Toast.makeText(mContext, "您移动到了" + (date.getMonth() + 1) + "月", Toast.LENGTH_SHORT).show();

            }
        });
        mCalendarView.setOnDateClickedListener((itemView, year, month, day) -> {
            Toast.makeText(mContext, String.format("%s年%s月%s日", year, month + 1, day), Toast.LENGTH_SHORT).show();

            // 在今天 和今天之后 可以点击
            if (year >= mThisYear && (month + 1) >= mThisMonth) {

                if ((month + 1) == mThisMonth) {

                    if (day >= mThisDay) {
                        // 任意按钮 第一次点击按钮
                        if (mLastDay == 0 && mCurrView == null) {
                            LinearLayout ll_layout = itemView.findViewById(R.id.ll_layout);
                            TextView tv_title = itemView.findViewById(R.id.tv_title);
                            ll_layout.setBackgroundResource(R.drawable.btn_bg_selector_orange_2dp);
                            tv_title.setText("开始");

                            // 上次 和 这次 相同
                        } else if (mLastDay == day) {
                            // 当前
                            LinearLayout ll_layout = itemView.findViewById(R.id.ll_layout);
                            TextView tv_title = itemView.findViewById(R.id.tv_title);

                            if (mThisDay == day) {

                                if (b) {
                                    ll_layout.setBackgroundResource(R.drawable.btn_bg_selector_orange_2dp);
                                    tv_title.setText("今天");
                                    b = false;
                                } else {
                                    ll_layout.setBackgroundResource(R.drawable.btn_bg_selector_red_2dp);
                                    tv_title.setText("开始");
                                    b = true;
                                }

                            } else {

                                if (b) {
                                    ll_layout.setBackgroundColor(Color.TRANSPARENT);
                                    tv_title.setText("");
                                    b = false;
                                } else {
                                    ll_layout.setBackgroundResource(R.drawable.btn_bg_selector_orange_2dp);
                                    tv_title.setText("开始");
                                    b = true;
                                }

                            }


                        } else {
                            // 之前
                            LinearLayout ll_layout_1 = mCurrView.findViewById(R.id.ll_layout);
                            TextView tv_title_1 = mCurrView.findViewById(R.id.tv_title);
                            // 当前
                            LinearLayout ll_layout = itemView.findViewById(R.id.ll_layout);
                            TextView tv_title = itemView.findViewById(R.id.tv_title);

                            if (mLastDay == mThisDay) {
                                ll_layout_1.setBackgroundResource(R.drawable.btn_bg_selector_red_2dp);
                                ll_layout.setBackgroundResource(R.drawable.btn_bg_selector_orange_2dp);
                                tv_title_1.setText("今天");
                                tv_title.setText("开始");

                            } else {
                                ll_layout_1.setBackgroundColor(Color.TRANSPARENT);
                                ll_layout.setBackgroundResource(R.drawable.btn_bg_selector_orange_2dp);
                                tv_title_1.setText("");
                                tv_title.setText("开始");
                            }
                        }

                        mLastDay = day;
                        mCurrView = itemView;

                        // 选中数据
                        mSelectYear = year;
                        mSelectMonth = (month + 1);
                        mSelectDay = day;

                        // 时间不对
                    } else {
                        Toast.makeText(mContext,
                                "请您选择" +
                                        String.format("%s月%s日", mThisMonth, mThisDay) +
                                        "之后的时间"
                                , Toast.LENGTH_SHORT).show();
                    }

                } else {

                    // 任意按钮 第一次点击按钮
                    if (mLastDay == 0 && mCurrView == null) {
                        LinearLayout ll_layout = itemView.findViewById(R.id.ll_layout);
                        TextView tv_title = itemView.findViewById(R.id.tv_title);
                        ll_layout.setBackgroundResource(R.drawable.btn_bg_selector_orange_2dp);
                        tv_title.setText("开始");

                        // 上次 和 这次 相同
                    } else if (mLastDay == day) {
                        // 当前
                        LinearLayout ll_layout = itemView.findViewById(R.id.ll_layout);
                        TextView tv_title = itemView.findViewById(R.id.tv_title);

                        if (mThisDay == day) {

                            if (b) {
                                ll_layout.setBackgroundResource(R.drawable.btn_bg_selector_orange_2dp);
                                tv_title.setText("今天");
                                b = false;
                            } else {
                                ll_layout.setBackgroundResource(R.drawable.btn_bg_selector_red_2dp);
                                tv_title.setText("开始");
                                b = true;
                            }

                        } else {

                            if (b) {
                                ll_layout.setBackgroundColor(Color.TRANSPARENT);
                                tv_title.setText("");
                                b = false;
                            } else {
                                ll_layout.setBackgroundResource(R.drawable.btn_bg_selector_orange_2dp);
                                tv_title.setText("开始");
                                b = true;
                            }

                        }


                    } else {
                        // 之前
                        LinearLayout ll_layout_1 = mCurrView.findViewById(R.id.ll_layout);
                        TextView tv_title_1 = mCurrView.findViewById(R.id.tv_title);
                        // 当前
                        LinearLayout ll_layout = itemView.findViewById(R.id.ll_layout);
                        TextView tv_title = itemView.findViewById(R.id.tv_title);


                        if (mLastDay == mThisDay) {
                            ll_layout_1.setBackgroundResource(R.drawable.btn_bg_selector_red_2dp);
                            ll_layout.setBackgroundResource(R.drawable.btn_bg_selector_orange_2dp);
                            tv_title_1.setText("今天");
                            tv_title.setText("开始");

                        } else {
                            ll_layout_1.setBackgroundColor(Color.TRANSPARENT);
                            ll_layout.setBackgroundResource(R.drawable.btn_bg_selector_orange_2dp);
                            tv_title_1.setText("");
                            tv_title.setText("开始");
                        }
                    }

                    mLastDay = day;
                    mCurrView = itemView;

                }

                // 年月不对
            } else {
                Toast.makeText(mContext,
                        "请您选择" +
                                String.format("%s年%s月%s日", mThisYear, mThisMonth, mThisDay) +
                                "之后的日期"
                        , Toast.LENGTH_SHORT).show();
            }

        });

        mVagueAdapter = new MyVagueAdapter(mContext, R.layout.layout_checkin_calendar_item);
        mVagueAdapter.setData(new HashMap<>());
        mCalendarView.setVagueAdapter(mVagueAdapter);
    }



}